/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conn.dao;

import conn.bean.BDepartamento;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author kuma-chin
 */
public class DDepartamento extends DBase {

    public BDepartamento getDepartamento(int id) {
        BDepartamento departamento = null;

        Statement stmt = null;
        ResultSet res = null;

        this.init_mysql();  // se inicia la conección a la base de datos

        try {

            stmt = conn.createStatement();
            res = stmt.executeQuery(
                    "SELECT idDepartamento, descripcion"
                    + " FROM Departamentos"
                    + " WHERE idDepartamento=" + id
                    + " ");

            departamento = new BDepartamento(res.getInt(1), res.getString(2));

            res.close();
            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }


        return departamento;
    }

    public List getmoddepartamento() {
        List Lista = new ArrayList();
        Statement stmt = null;
        ResultSet res = null;

        BDepartamento bdepartamento = null;

        this.init_mysql();  // se inicia la conección a la base de datos

        try {
            stmt = conn.createStatement();
            res = stmt.executeQuery(
                    "select idDepartamento, descripcion from departamento;");

            while (res.next()) {

                bdepartamento = new BDepartamento(res.getInt(1),res.getString(2));
                Lista.add(bdepartamento);
            }
            res.close();
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
        return Lista;
    }

    public String getStrDep(int dep) {
        Statement stmt = null;
        ResultSet res = null;

        this.init_mysql();  // se inicia la conección a la base de datos
        String depa = null;
        try {

            stmt = conn.createStatement();
            res = stmt.executeQuery("select descripcion from departamento where idDepartamento = '" + dep + "';");

            if (res.next()) {

                depa = res.getString(1);
            }

            res.close();
            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
        return depa;
    }
    public int getIdDep(String strdep) {
        Statement stmt = null;
        ResultSet res = null;

        this.init_mysql();  // se inicia la conección a la base de datos
        int intdepa = 0;
        try {

            stmt = conn.createStatement();
            res = stmt.executeQuery("select idDepartamento,descripcion from departamento where descripcion = '" + strdep + "';");

            if (res.next()) {

                intdepa = res.getInt(1);
            }

            res.close();
            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
        return intdepa;
    }

    


}
